package com.smartcampusbackend.service.impl;

import com.smartcampusbackend.model.TitleApplication;
import com.smartcampusbackend.mapper.TitleApplicationMapper;
import com.smartcampusbackend.mapper.UserMapper;
import com.smartcampusbackend.model.User;
import com.smartcampusbackend.service.TitleApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TitleApplicationServiceImpl implements TitleApplicationService {
    @Autowired
    private TitleApplicationMapper titleApplicationMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public String applyForTitle(TitleApplication application) {
        application.setStatus("审批中");
        application.setCreateTime(LocalDateTime.now());
        application.setUpdateTime(LocalDateTime.now());
        titleApplicationMapper.insert(application);
        return "职称申请提交成功，请等待审核";
    }

    @Override
    public List<TitleApplication> getMyApplications(Integer teacherId) {
        return titleApplicationMapper.selectList(
            new QueryWrapper<TitleApplication>().eq("teacher_id", teacherId).orderByDesc("create_time")
        );
    }

    @Override
    public List<Map<String, Object>> listTitleApplications(int page, int size) {
        int offset = (page - 1) * size;
        List<TitleApplication> applications = titleApplicationMapper.selectList(
            new QueryWrapper<TitleApplication>().orderByDesc("create_time").last("LIMIT " + offset + ", " + size)
        );
        List<Map<String, Object>> result = new ArrayList<>();
        for (TitleApplication application : applications) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", String.valueOf(application.getId()));
            map.put("teacherId", String.valueOf(application.getTeacherId()));
            map.put("name", application.getName());
            map.put("gender", application.getGender());
            map.put("currentTitle", application.getCurrentTitle());
            map.put("applyTitle", application.getApplyTitle());
            map.put("department", application.getDepartment());
            map.put("position", application.getPosition());
            map.put("attachment", application.getAttachment());
            map.put("status", application.getStatus());
            map.put("reason", application.getReason());
            map.put("createTime", application.getCreateTime());
            map.put("updateTime", application.getUpdateTime());
            User teacher = userMapper.selectById(application.getTeacherId());
            if (teacher != null) {
                map.put("teacherName", teacher.getRealName());
                map.put("teacherNumber", teacher.getUsername());
            } else {
                map.put("teacherName", "未知");
                map.put("teacherNumber", "未知");
            }
            result.add(map);
        }
        return result;
    }

    @Override
    @Transactional
    public String approveTitle(Long applicationId) {
        TitleApplication application = titleApplicationMapper.selectById(applicationId);
        if (application == null) {
            return "职称申请不存在";
        }
        application.setStatus("已通过");
        application.setUpdateTime(LocalDateTime.now());
        titleApplicationMapper.updateById(application);
        return "职称申请已通过";
    }

    @Override
    @Transactional
    public String rejectTitle(Long applicationId, String reason) {
        TitleApplication application = titleApplicationMapper.selectById(applicationId);
        if (application == null) {
            return "职称申请不存在";
        }
        application.setStatus("未通过");
        application.setReason(reason);
        application.setUpdateTime(LocalDateTime.now());
        titleApplicationMapper.updateById(application);
        return "职称申请已拒绝";
    }
} 